<template>
  <div class="NavMemu">
    <el-menu
      :default-active="defaultActive"
      class="menu"
      background-color="#0c2135"
      :collapse="collapse"
      text-color="#b7bdc3"
      active-text-color="#0a60bd"
      :collapse-transition="true"
    >
      <el-menu-item
        class="logo"
        style="background: #0c2135 !important; padding: 0"
      >
        <img src="~@/assets/images/logo.svg" alt="logo" />
        <span class="title" v-if="!collapse">后台管理系统</span>
      </el-menu-item>
      <template v-for="item in userMenus" :key="item.id">
        <!-- 可以展开的菜单 -->
        <template v-if="item.type === 1">
          <el-sub-menu :index="item.id + ''">
            <!-- 可以展开的菜单的标题 -->
            <template #title>
              <component :is="item.icon" />
              <span v-if="!collapse">{{ item.name }}</span>
            </template>
            <!-- 展开后的每一个项 -->
            <template v-for="subitem in item.children" :key="subitem.id">
              <el-menu-item
                :index="subitem.id + ''"
                @click="handleMenuItemClick(subitem)"
              >
                <component
                  :is="subitem.icon"
                  v-if="subitem.icon"
                  class="icon"
                />
                <span>{{ subitem.name }}</span>
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <template v-if="item.type === 2">
          <el-menu-item :index="item.id + ''">
            <component :is="item.icon" v-if="item.icon" class="icon" />
            <span>{{ item.name }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script lang="ts">
import { useStore } from '@/store'
import { pathMapMenu } from '@/utils/mapMenusRouter'
import { computed, defineComponent } from 'vue'
import { useRouter, useRoute } from 'vue-router'
export default defineComponent({
  props: {
    collapse: {
      type: Boolean,
      default: false
    }
  },
  setup() {
    const store = useStore()
    const router = useRouter()
    const route = useRoute()
    const userMenus = computed(() => store.state.login.userMenus)
    const defaultActive = computed(() => {
      const menu = pathMapMenu(userMenus.value, route.path)
      return menu.id + ''
    })
    const handleMenuItemClick = (item: any) => {
      router.push({
        path: item.url ?? '/not-found'
      })
    }
    return { userMenus, handleMenuItemClick, defaultActive }
  }
})
</script>

<style scoped lang="less">
.NavMemu {
  width: 100%;
  height: 100%;
  .menu {
    width: 100%;
    height: 100%;
    .logo {
      text-align: center;
      img {
        width: 30px;
        margin-left: 15px;
      }
      span {
        font-size: 16px;
        font-weight: 700;
        color: white;
        margin-left: 10px;
      }
    }
    .icon {
      width: 16px;
      height: 16px;
      // margin-right: 10px;
    }
    span {
      margin-left: 10px;
    }
  }
  .el-menu {
    border-right: none;
  }
  // 目录
  .el-menu-item {
    background: #1c2f40 !important;
  }
}
</style>
